<?php

namespace Tbf\Bundle\TbfBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class SymfonyLazyServiceCompilerPass implements CompilerPassInterface
{
    /**
     * You can modify the container here before it is dumped to PHP code.
     *
     * @param ContainerBuilder $container
     *
     * @api
     */
    public function process(ContainerBuilder $container)
    {
        if (!method_exists('\Symfony\Component\DependencyInjection\Definition','setLazy')) {
            return;
        }
        $lazy_service_id_list = array(
            'doctrine.orm.security.user.provider', //如果用户没有登录就不需要这个
            'twig',
            'templating.engine.twig',            //不一定需要twig模板引擎
            'translator.default',                //不一定需要做翻译
            'security.authentication.manager',
            'security.access.decision_manager', //如果是firewall允许的就不需要这一步
            'session'                           //某些请求不需要session
        );
        foreach ($lazy_service_id_list as $lazy_service_id) {
            if (!$container->hasDefinition($lazy_service_id)) {
                return;
            }
            $container->getDefinition($lazy_service_id)->setLazy(true);
        }
    }
}
